/*
 * IC Project - a prototype to test a hypothesis of collective intelligence
 * Copyright (C) 2012 Paolo Conte - paolo@concavi.net
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This code is derived from the project ojoverse (2008, same author)
 * under the license GPL2. See <http://www.gnu.org/licenses/>.
 */

package org.ix.utils;

import java.util.List;

public class Paginator {

    /**
     * Create a Pager for the current page
     * @param elems Elements to paginate
     * @param page Page number
     * @param entries Number of entries for page
     * @return
     */
    public Pager paginate(List elems, int page, int entries) {
        int pages = elems.size() / entries;
        int from = entries * (page - 1);
        int size = elems.size();
        if (size > entries) {
            size = entries;
        }
        int to = from + size;
        List sublist = elems.subList(from, to);
        Pager pager = new Pager(sublist, pages);
        return pager;
    }

    /**
     * A simple container for paging informations
     */
    public static class Pager {
        // the elements to show

        public List sublist = null;
        // total pages number
        public int pages = 0;

        public Pager(List sublist, int pages) {
            this.sublist = sublist;
            this.pages = pages;
        }
    }
}
